package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import domain.Department;
import domain.School;
import domain.Teacher;
import exception.BysjException;
import service.DepartmentService;
import service.TeacherService;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//put,http://localhost:8080/TeacherUpdate.ctl
@WebServlet("/DepartmentUpdate.ctl")
public class DepartmentUpdateCtl extends HttpServlet {
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        response.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message_jsonObj,以便往前端响应信息
        //设置响应字符编码为utf-8
        //将request body转换成JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        //将request body解析成JSONObject对象，可能含有分页信息
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        //如果请求body为空
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try{
            String description = req_jsonObj.getString("description");
            String no = req_jsonObj.getString("no");
            String remarks = req_jsonObj.getString("remarks");
            JSONObject department_jsonObj = req_jsonObj.getJSONObject("school");
            School school = null;
            //如果有"pagination"键
            if (department_jsonObj != null){
                //将描述分页的JSONObject对象转换为java对象
                school = department_jsonObj.toJavaObject(School.class);
            }
            Department department = new Department(description,no,remarks,school);
            boolean updated = DepartmentService.getInstance().update(department);
            message_jsonObj.put("updated",updated);
            message_jsonObj.put("records","修改了一条信息");
            //分页获得所有学院
            //响应message_jsonObj到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
